func fourSum(nums []int, target int) [][]int {
    ans := make([][]int, 0)
    n := len(nums)
    sort.Ints(nums)
    for i:=0; i<n-3; i++ {
        n1 := nums[i]
        
        if i>0 && nums[i-1] == n1 {
            continue
        }
        for j:=i+1; j<n-2; j++ {
            
            n2 := nums[j]
            if j > i+1 && nums[j-1] == n2{
                continue
            }
            l, r := j+1, n-1
            
            for l < r {
                n3, n4 := nums[l], nums[r]
                sum := n1 + n2 + n3 + n4
                
                if sum == target {
                    ans = append(ans, []int{n1, n2, n3, n4})
                    for l<n-1 && nums[l] == n3 {
                        l++
                    }
                    for r>0 && nums[r] == n4 {
                        r--
                    }
                } else if sum > target {
                    r--
                } else {
                    l++
                }
            }
            
        }
    }
    return ans
}